﻿
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System;

using System.Linq;
using Communergy.UnitTesting;
using Communergy.NationalGridDataServices;

namespace CommunergyLogicTests
{
    
    
    /// <summary>
    ///This is a test class for GridDataHelperTest and is intended
    ///to contain all GridDataHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GridDataHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for LiveCarbonIntensityFromXml
        ///</summary>
        [TestMethod()]
        public void LiveCarbonIntensityFromXmlTest_amount()
        {
            var expectedData = new decimal( 0.603226192045817);
            string liveCarbonIntensityXml = SampleLiveGridData.SampleLiveCarbonIntensity;         
            var actual = GridDataHelper.LiveCarbonIntensityFromXml(liveCarbonIntensityXml);
            Assert.AreEqual(expectedData, actual.amount);
        }

        [TestMethod()]
        public void LiveCarbonIntensityFromXmlTest_startDate()
        {
            var expectedData = new DateTime(2010, 01, 11, 22, 20, 0);
            string liveCarbonIntensityXml = SampleLiveGridData.SampleLiveCarbonIntensity;
            var actual = GridDataHelper.LiveCarbonIntensityFromXml(liveCarbonIntensityXml);
            Assert.AreEqual(expectedData, actual.startDateGMT);
        }

        [TestMethod()]
        public void LiveCarbonIntensityFromXmlTest_endDate()
        {
            var expectedData = new DateTime(2010, 01, 11, 22, 25, 0);
            string liveCarbonIntensityXml = SampleLiveGridData.SampleLiveCarbonIntensity;
            var actual = GridDataHelper.LiveCarbonIntensityFromXml(liveCarbonIntensityXml);
            Assert.AreEqual(expectedData, actual.endDateGMT);
        }

        [TestMethod()]
        public void LiveCarbonIntensityFromXmlTest_RollingMean_Amount()
        {
            var expectedData = new decimal(0.588081347447252783);
            string liveCarbonIntensityXml = SampleLiveGridData.SampleLiveCarbonIntensity;
            var actual = GridDataHelper.LiveCarbonIntensityFromXml(liveCarbonIntensityXml);
            Assert.AreEqual(expectedData, actual.hhRollingMean.value);
        }


        [TestMethod()]
        public void LiveCarbonIntensityFromXmlTest_RollingMean_startDate()
        {
            var expectedData = new DateTime(2010, 01, 4, 22, 00, 0);
            string liveCarbonIntensityXml = SampleLiveGridData.SampleLiveCarbonIntensity;
            var actual = GridDataHelper.LiveCarbonIntensityFromXml(liveCarbonIntensityXml);
            Assert.AreEqual(expectedData, actual.hhRollingMean.startDateGMT);
        }

        [TestMethod()]
        public void LiveCarbonIntensityFromXmlTest_RollingMean_endDate()
        {
            var expectedData = new DateTime(2010, 01, 11, 22, 00, 0);
            string liveCarbonIntensityXml = SampleLiveGridData.SampleLiveCarbonIntensity;
            var actual = GridDataHelper.LiveCarbonIntensityFromXml(liveCarbonIntensityXml);
            Assert.AreEqual(expectedData, actual.hhRollingMean.endDateGMT);
        }

        [TestMethod()]
        public void LiveCarbonIntensityFromXmlTest_OfficialStandard()
        {
            var expectedData =(decimal)0.527;
            string liveCarbonIntensityXml = SampleLiveGridData.SampleLiveCarbonIntensity;
            var actual = GridDataHelper.LiveCarbonIntensityFromXml(liveCarbonIntensityXml);
            Assert.AreEqual(expectedData, actual.officialStandard.value);
        }


        /// <summary>
        ///A test for GENERATION_BY_FUEL_TYPE_TABLEFromXml
        ///</summary>
        [TestMethod()]
        public void GENERATION_BY_FUEL_TYPE_TABLEFromXmlTest_Current_Total()
        {
            string GENERATION_BY_FUEL_TYPE_TABLEXml = SampleLiveGridData.SampleLiveFuelMix;
            var expected = 44509;
            GENERATION_BY_FUEL_TYPE_TABLE actual;
            actual = GridDataHelper.GENERATION_BY_FUEL_TYPE_TABLEFromXml(GENERATION_BY_FUEL_TYPE_TABLEXml);
            Assert.AreEqual(expected, actual.INST.TOTAL);

        }

        [TestMethod()]
        public void GENERATION_BY_FUEL_TYPE_TABLEFromXmlTest_Current_SubTotals()
        {
            string GENERATION_BY_FUEL_TYPE_TABLEXml = SampleLiveGridData.SampleLiveFuelMix;
            var expected = 44509;
            GENERATION_BY_FUEL_TYPE_TABLE actual;
            actual = GridDataHelper.GENERATION_BY_FUEL_TYPE_TABLEFromXml(GENERATION_BY_FUEL_TYPE_TABLEXml);
            var subtotal = actual.INST.FUEL.Sum(f => f.VAL);
            Assert.AreEqual(expected, subtotal);
        }

        [TestMethod()]
        public void GENERATION_BY_FUEL_TYPE_TABLEFromXmlTest_Current_SubTotals_Percentage()
        {
            string GENERATION_BY_FUEL_TYPE_TABLEXml = SampleLiveGridData.SampleLiveFuelMix;
            var expected = 100;
            GENERATION_BY_FUEL_TYPE_TABLE actual;
            actual = GridDataHelper.GENERATION_BY_FUEL_TYPE_TABLEFromXml(GENERATION_BY_FUEL_TYPE_TABLEXml);
            var subtotal = actual.INST.FUEL.Sum(f => f.PCT);
            Assert.AreEqual(expected, subtotal);
        }

        /// <summary>
        ///A test for GetRemoteXml
        ///</summary>
        [TestMethod()]
        public void GetRemoteXmlTest_CarbonIntensity()
        {
            string url = "http://www.realtimecarbon.org/ws/electricity/services/co2profile/5minute/item";
            
            string actual;
            actual = GridDataHelper.GetRemoteXml(url);

            var liveCarbonIntensity = GridDataHelper.LiveCarbonIntensityFromXml(actual);
            Assert.IsTrue(liveCarbonIntensity.startDateGMT > DateTime.Now.AddMinutes(-5));
            Assert.IsTrue(liveCarbonIntensity.amount > 0 && liveCarbonIntensity.amount<1);
        }

        /// <summary>
        ///A test for LiveCarbonIntensityFromXml
        ///</summary>
        [TestMethod()]
        public void LiveCarbonIntensityFromXmlTest_rubbishXmlData()
        {
            string liveCarbonIntensityXml = UnitTestHelper.RandomString(25);
            electricityCO2Item expected = null; // TODO: Initialize to an appropriate value
            electricityCO2Item actual;
            actual = GridDataHelper.LiveCarbonIntensityFromXml(liveCarbonIntensityXml);
            Assert.AreEqual(expected, actual);

        }
    }
}

